﻿using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;

namespace ExpertApplication.DatabaseModel
{
    public class ExpertInitializer : DropCreateDatabaseAlways<ExpertContext>
    {
        protected override void Seed(ExpertContext context)
        {
            var levels = new List<QuestionLevel>();
            for(int i = 0; i < 10; i++)
                levels.Add(new QuestionLevel { Level = i });

            foreach(QuestionLevel questionLevel in levels)
                context.QuestionLevels.Add(questionLevel);
            context.SaveChanges();

            var groups = new[]
                             {
                                 new QuestionGroup {Name = "algorithms"},
                                 new QuestionGroup {Name = "basic"},
                                 new QuestionGroup {Name = "Data Structure"},
                                 new QuestionGroup {Name = "logic"},
                                 new QuestionGroup {Name = "C#"},
                                 new QuestionGroup {Name = "Razor"},
                                 new QuestionGroup {Name = "OOP"},
                                 new QuestionGroup {Name = "Patterns"}
                             };
            foreach(QuestionGroup questionGroup in groups)
                context.QuestionGroups.Add(questionGroup);

            var answersFor1Question = new[]
                                          {
                                              new Answer {Text = "answer 1_1", Weight = 0.25f},
                                              new Answer {Text = "answer 1_2", Weight = -0.2f},
                                              new Answer {Text = "answer 1_3", Weight = 1f},
                                              new Answer {Text = "answer 1_4", Weight = -0.5f},
                                              new Answer {Text = "answer 1_5", Weight = -0.7f}
                                          };
            var answersFor2Question = new[]
                                          {
                                              new Answer {Text = "answer 2_1", Weight = 0f},
                                              new Answer {Text = "answer 2_2", Weight = 0f},
                                              new Answer {Text = "answer 2_3", Weight = 0f},
                                              new Answer {Text = "answer 2_4", Weight = 0f},
                                              new Answer {Text = "answer 2_5", Weight = 1.0f}
                                          };

            var answerFor3Question = new[]
                                         {
                                             new Answer{Text="answer 3_1",Weight =1f},
                                             new Answer{Text="answer 3_2",Weight =0f},
                                             new Answer{Text="answer 3_3",Weight =0f},
                                             new Answer{Text="answer 3_4",Weight =0f},
                                             new Answer{Text="answer 3_5",Weight =0f},
                                             new Answer{Text="answer 3_6",Weight =0f},
                                         };
            var questions = new[]
                                {new Question
                                {
                                    Answers = answersFor1Question,
                                    QuestionGroups = groups,
                                    Level = context.QuestionLevels.SingleOrDefault(l => l.Level == 0),
                                    Text = "Question 1",
                                    Weight = 100.0f,
                                    IsMultiSelected = true
                                },
                                new Question
                                {
                                    Answers = answersFor2Question,
                                    QuestionGroups = groups,
                                    Level = context.QuestionLevels.SingleOrDefault(l => l.Level == 0),
                                    Text = "Quesiton 2",
                                    Weight = 25.0f,
                                    IsMultiSelected = false
                                },
                                new Question
                                {
                                    Answers = answerFor3Question,
                                    QuestionGroups = groups,
                                    Level = context.QuestionLevels.SingleOrDefault(l => l.Level == 1),
                                    Text = "Question 3",
                                    Weight = 20.5f,
                                    IsMultiSelected = false
                                }
                                };
            foreach(Question question in questions)
            {
                context.Questions.Add(question);
            }

            context.SaveChanges();
        }
    }
}